﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace GeoSketch2.Core
{
    public partial class BaseToolbar : UserControl
    {
        #region Fields
        /// <summary>
        /// The original height of the toolbar, used for expanding and collapsing it
        /// </summary>
        protected int ToolbarHeight;

        bool _TitlebarMouseDown;//True if the mouse down on the title bar (panel)
        Point _PreviousClic; //The coordinates of the previous clic
        #endregion

        #region Constructor
        /// <summary>
        /// The base class for the toolbars of geosketch, contains a title bars and some buttons
        /// </summary>
        public BaseToolbar()
        {
            InitializeComponent();

            ToolbarHeight = this.Height;
            _TitlebarMouseDown = false;

            this.Resize += new EventHandler(GeoToolbar_Resize);
            this.panel_title.MouseDown += new MouseEventHandler(panel_title_MouseDown);
            this.panel_title.MouseMove += new MouseEventHandler(panel_title_MouseMove);
            this.panel_title.MouseUp += new MouseEventHandler(panel_title_MouseUp);
            this.label_Title.MouseDown += new MouseEventHandler(panel_title_MouseDown);
            this.label_Title.MouseMove += new MouseEventHandler(panel_title_MouseMove);
            this.label_Title.MouseUp += new MouseEventHandler(panel_title_MouseUp);
            this.MouseDown += new MouseEventHandler(panel_title_MouseDown);
            this.MouseMove += new MouseEventHandler(panel_title_MouseMove);
            this.MouseUp += new MouseEventHandler(panel_title_MouseUp);
        } 
        #endregion

        #region Events
        #region Moving the toolbar when draging the title bar
        void panel_title_MouseUp(object sender, MouseEventArgs e)
        {
            _TitlebarMouseDown = false;
        }

        void panel_title_MouseMove(object sender, MouseEventArgs e)
        {
            if (_TitlebarMouseDown)
            {
                this.Left +=  MousePosition.X-_PreviousClic.X ;
                this.Top += MousePosition.Y - _PreviousClic.Y;
                _PreviousClic = MousePosition;
            }
        }

        void panel_title_MouseDown(object sender, MouseEventArgs e)
        {
            _TitlebarMouseDown = true;
            _PreviousClic = MousePosition;
            this.BringToFront();
        } 
        #endregion

        #region When resizing the control, the title bar will resize itself
        void GeoToolbar_Resize(object sender, EventArgs e)
        {
            panel_title.Width = this.Width;
            button_hide.Left = panel_title.Width - button_hide.Width - 2*Vars.Setup.Toolbars.SpaceBetweenTitleButtons;
            button_expand.Left = button_hide.Left - button_expand.Width - Vars.Setup.Toolbars.SpaceBetweenTitleButtons;
            button_collapse.Left = button_expand.Left - button_collapse.Width - Vars.Setup.Toolbars.SpaceBetweenTitleButtons;
        }  
        #endregion

        #region Clicks on the buttons
        /// <summary>
        /// The action when the hide button is clicked
        /// </summary>
        protected virtual void button_hide_Click(object sender, EventArgs e)
        {
            this.Visible = false;
            ((ToolStripMenuItem)this.Tag).Checked = false;
        }

        private void button_expand_Click(object sender, EventArgs e)
        {
            this.Height = ToolbarHeight;
        }

        private void button_collapse_Click(object sender, EventArgs e)
        {
            this.Height = panel_title.Height;
        } 
        #endregion
        #endregion

        #region Properties
        /// <summary>
        /// MUST be set in the form, or the buttons won't work
        /// Sets the GeoDisplayBox to every buttons of the toolbar so they "know" on which control they have to act
        /// </summary>
        public BaseGeoDisplayBox DisplayBox
        {
            set
            {
                foreach (Control control in this.Controls)
                {
                    if (control.GetType().BaseType == typeof(BaseToolbarButton) || control.GetType().BaseType.BaseType == typeof(BaseToolbarButton))
                    {
                        ((BaseToolbarButton)control).DisplayBox = value;
                    }
                }
            }
        }

        /// <summary>
        /// Sets the title of the toolbar
        /// </summary>
        public string Title
        {
            get
            {
                return label_Title.Text;
            }
            set
            {
                label_Title.Text = value;
            }
        } 
        #endregion
    }
}
